<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容 - 头部 -->
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
        <TreeTools :treeNode="company" :isRoot="true" @addDepts="addDepts"></TreeTools>
        <!-- 放置 - el-tree -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
           <TreeTools slot-scope=" { data }" :treeNode="data" @delDepts="getDepartments" @addDepts="addDepts"
           @editDepts="editDepts"></TreeTools>
        </el-tree>
      </el-card>
    </div>
    <!-- 新增弹层组件 -->
    <AddDept ref="addDept" :showDialog.sync="showDialog" :treeNode="node" @addDepts="getDepartments"></AddDept>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  created () {
    this.getDepartments()
  },
  data () {
    return {
      company: {  },
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false, // 默认不显示弹层
      node: null, // 几率当前点击的node节点
      loading: false // 用来控制进度弹层的显示和隐藏
    }
  },
  methods: {
    async getDepartments () {
      this.loading = true
      const res = await getDepartments()
      console.log(res);
      this.company = { name: res.companyName, manager: '负责人', id: ''}
      this.departs = tranListToTreeDate(res.depts, '')// 需要将其转化成树形结构
      this.loading = false
    },
    addDepts (node) {
      this.showDialog = true // 显示弹层
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true // 显示弹层
      this.node = node
      // 父组件调用子组件的方法
      this.$refs.addDept.getDepartDatail(node.id)
    }
  }
}
</script>

<style scoped>
  .tree-card {
    padding: 30px 140px;
    font-size: 14px;
  }
</style>
